package com.jxzn.locust.controller.admin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jxzn.locust.dto.ResultDTO;
import com.jxzn.locust.model.Role;
import com.jxzn.locust.model.RoleResource;
import com.jxzn.locust.model.SysResource;
import com.jxzn.locust.service.AbstractService;
import com.jxzn.locust.service.ResourceService;
import com.jxzn.locust.service.RoleResourceService;
import com.jxzn.locust.service.RoleService;
import com.jxzn.locust.support.Auth;
import com.jxzn.locust.support.Module;
import com.jxzn.locust.support.OperateAction;

@Controller
@RequestMapping("/bg/role")
@Module("角色管理")
public class RoleController extends NamedEntityController<Role> {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    RoleService roleService;
    @Autowired
    ResourceService resourceService;
    @Autowired
    RoleResourceService roleResourceService;

    @Override
    public AbstractService<Role> getService() {
        return roleService;
    }

    @Override
    public String getFolder() {
        return "role";
    }

    @GetMapping("/resource")
    @Auth(action = OperateAction.VIEW)
    public String resource(Model model, @RequestParam("id") int id) {

        List<SysResource> allList = resourceService.findAll();

        List<SysResource> srList = resourceService.findByRoleId(id);
        for (SysResource sr : srList) {
            for (SysResource all : allList) {
                if (all.getId().equals(sr.getId())) {
                    all.setSelected(true);
                }
            }
        }

        Collections.sort(allList);
        Map<String, List<SysResource>> moduleMap = new HashMap<>();
        for (SysResource sr : allList) {
            if (!moduleMap.containsKey(sr.getModule())) {
                moduleMap.put(sr.getModule(), new ArrayList<>());
            }
            moduleMap.get(sr.getModule()).add(sr);
        }
        model.addAttribute("moduleMap", moduleMap);
        model.addAttribute("roleId", id);
        return "/bg/role/resource";
    }

    @PostMapping("/resource")
    @ResponseBody
    @Auth(action = OperateAction.EDIT)
    public ResultDTO<?> resource(Model model, @RequestParam("roleId") int roleId, @RequestParam(value = "resource[]", required = false) String[] uuids,
            @RequestParam(value = "resource", required = false) String rid) {
        Optional<Role> optional = roleService.findById(roleId);
        if (!optional.isPresent() || optional.get().getDeleted()) {
            return ResultDTO.param("roleId");
        }

        List<RoleResource> rrList = new ArrayList<>();
        if (null == uuids) {
            if (StringUtils.isNotBlank(rid)) {
                rrList.add(new RoleResource(roleId, rid));
            }
        } else {
            for (String uuid : uuids) {
                rrList.add(new RoleResource(roleId, uuid));
            }
        }

        roleResourceService.saveAll(optional.get(), rrList);

        return ResultDTO.success();
    }
}
